package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.query.LoginLogPageParams;
import com.hr.domain.query.LoginLogPageResult;
import com.hr.domain.vo.UserLoginLogVO;
import com.hr.mapper.UserLoginLogMapper;
import com.hr.service.UserLoginLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 86150
* @description 针对表【user_login_log(用户登录日志表)】的数据库操作Service实现
* @createDate 2025-10-20 14:22:00
*/
@Slf4j
@Service
@AllArgsConstructor
public class UserLoginLogServiceImpl implements UserLoginLogService{

    private final UserLoginLogMapper userLoginLogMapper;

    /**
     * 分页获取用户登录日志列表
     * @param queryParams
     * @return
     */
    @Override
    public LoginLogPageResult PageLoginLogList(LoginLogPageParams queryParams) {
        //开始分页
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        //传入分页参数
        List<UserLoginLogVO> userLoginLogList = userLoginLogMapper.getUserLoginLogPageList(queryParams);
        //强转为Page<UserLoginLogVO>
        Page<UserLoginLogVO> page = (Page<UserLoginLogVO>) userLoginLogList;
        //返回结果
        return new LoginLogPageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除用户登录日志
     * @param logId
     * @return
     */
    @Override
    public String deleteLoginLog(Long logId) {
        int row = userLoginLogMapper.deleteLoginLog(logId);
        return row > 0 ? "删除用户登录日志成功" : "删除用户登录日志失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteLoginLogBatch(List<Long> logIds) {
        //参数校验,判断传入的参数是否正确
        if (logIds == null || logIds.isEmpty()){
            return "未选择任何记录";
        }
        try {
            int deletedRows = userLoginLogMapper.deleteLoginLogBatch(logIds);
            if (deletedRows != logIds.size()){
                log.error("批量删除不完全，期望删除 {} 条，实际删除 {} 条", logIds.size(), deletedRows);
                return "部分删除失败";
            }
        } catch (Exception e) {
            log.error("批量删除异常", e);
            throw new RuntimeException("删除失败", e);
        }
          return "批量删除成功";
    }
}




